﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace WebUI.pro
{
    public partial class Details : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["url"] != null)
                {

                    Model.URLInfo modurl = BLL.URLInfo.GetURLInfoPK(Convert.ToInt32(Request.QueryString["url"].ToString()));
                    this.txtTitle.Text = modurl.Title;
                    this.txtRemark.Text = modurl.Remark;
                    this.txtURL.Text = modurl.Url;
                    this.img.ImageUrl = "~/" + modurl.Img;
                    bindFavorite();
                    this.ddlFavorite.SelectedValue = modurl.FID.ToString();
                }
            }
        }

        private void bindFavorite()
        {
            IList<Model.Favorite> list = BLL.Favorite.GetTopFavorite(Convert.ToInt32(Session["uid"].ToString()));
            if (list != null)
            {
                foreach (Model.Favorite modfav in list)
                {
                    ListItem favitem = new ListItem(modfav.FName, modfav.FID.ToString());
                    this.ddlFavorite.Items.Add(favitem);
                    bindChildFavorite(favitem);
                }
            }
        }
        //使用递归，使数据以数型绑定
        private void bindChildFavorite(ListItem item)
        {

            IList<Model.Favorite> childlist = BLL.Favorite.GetChildFavorite(Convert.ToInt32(item.Value.ToString()));
            if (childlist != null)
            {
                foreach (Model.Favorite modfav in childlist)
                {
                    ListItem favitem = new ListItem(item.Text + "\\" + modfav.FName, modfav.FID.ToString());
                    this.ddlFavorite.Items.Add(favitem);
                    bindChildFavorite(favitem);
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Model.URLInfo modurl = new Model.URLInfo();
            modurl.Title = this.txtTitle.Text;
            modurl.Remark = this.txtRemark.Text;
            modurl.Url = this.txtURL.Text;
            modurl.FID = Convert.ToInt32(this.ddlFavorite.SelectedValue);
            modurl.URID = Convert.ToInt32(Request.QueryString["url"].ToString());
            string imgpath = this.img.ImageUrl;
            if (UploadImage(ref imgpath))
            {
                modurl.Img = imgpath;

            }
            Response.Write(modurl.Img);
            BLL.URLInfo.UpdateURLInfo(modurl);
        }
        //上传图片
        private bool UploadImage(ref string imgpath)
        {

            if (this.fileImg.PostedFile != null && this.fileImg.PostedFile.FileName.ToString().Trim() != "")
            {


                string photoName1 = fileImg.PostedFile.FileName; //获取初始文件名
                int i = photoName1.LastIndexOf("."); //取得文件名中最后一个"."的索引
                string newext = photoName1.Substring(i); //获取文件扩展名
                if (newext != ".gif" && newext != ".jpg" && newext != ".jpeg" && newext != ".bmp" && newext != ".png")
                {
                    Response.Write("文件格式不正确!");
                    Response.End();
                }
                DateTime now = DateTime.Now; //获取系统时间

                imgpath = now.Millisecond.ToString() + "_" + fileImg.PostedFile.ContentLength.ToString() + newext; //重新为文件命名,时间毫秒部分+文件大小+扩展名

                try
                {
                    fileImg.PostedFile.SaveAs(Server.MapPath("../photos") + "\\" + imgpath); // 保存文件到路径,用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用""代替
                    imgpath = "photos/" + imgpath;
                    return true;
                }
                catch (Exception ex)
                {
                    Console.Write(ex.Message);
                    return false;
                }

            }
            return false;


        }
    }
}
